//
//  SuggestViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-3-25.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "SuggestViewController.h"
#import "SinaWeiboManager.h"

@interface SuggestViewController ()

@end

@implementation SuggestViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sinaWeiboLogStateDidChange) name:kSinaWeiboLogStateDidChange object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [suggestTextView becomeFirstResponder];
}

- (IBAction)commitSuggest:(id)sender
{
    [suggestTextView resignFirstResponder];
    if (suggestTextView.text.length <= 5) {
        ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"多写一点吧"] autorelease];
        [promptView show];
        return;
    }
    NSString *status = [NSString stringWithFormat:@"#意见反馈#%@@中邮阅读",suggestTextView.text];
    if ([[[SinaWeiboManager defaultManager] sinaweibo] isAuthValid]) {
        sendButton.enabled = NO;
        [self showLoadingView];
        [[[SinaWeiboManager defaultManager] sinaweibo] requestWithURL:@"statuses/upload.json" params:[NSMutableDictionary dictionaryWithObjectsAndKeys:status, @"status",[UIImage imageNamed:@"icon@2x.png"],@"pic", nil] httpMethod:@"POST" delegate:self];
    }
    else {
        [[[SinaWeiboManager defaultManager] sinaweibo] logIn];
    }
}

- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result
{
    [self removeloadingView];
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"感谢您的反馈"] autorelease];
    [promptView show];
    [self navBack:self];
}

- (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error
{
    sendButton.enabled = YES;
    [self removeloadingView];
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"发送失败"] autorelease];
    [promptView show];
}

- (void)sinaWeiboLogStateDidChange
{
    if ([[[SinaWeiboManager defaultManager] sinaweibo] isAuthValid]) {
        [self commitSuggest:nil];
    }
}

- (IBAction)navBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:contentView.bounds] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [suggestTextView release];
    [contentView release];
    [sendButton release];
    [super dealloc];
}
- (void)viewDidUnload {
    [suggestTextView release];
    suggestTextView = nil;
    [contentView release];
    contentView = nil;
    [sendButton release];
    sendButton = nil;
    [super viewDidUnload];
}
@end
